const bodyParser = require("body-parser");
const JWt = require("jsonwebtoken");
const key = "awdknemflawl";
const { list , userList} = require('./mock')
module.exports = (app) => {
  app.use(bodyParser.json());

  app.post("/api/login", (req, res) => {
    const { username, password } = req.body;
    const result = userList.find(
      (v) => v.username === username && v.password === password
    );
    if (result) {
      res.send({
        code: 200,
        token: JWt.sign(
          {
            username: result.username,
            nickname: result.nickname,
            avator: result.avator,
          },
          key
        ),
        data: {
          username: result.username,
          nickname: result.nickname,
          avator: result.avator,
        },
      });
    } else {
      res.status(403).send({
        code: 403,
        message: "账户密码错误",
      });
    }
  });


  app.get('/api/list', (req, res) => {
    res.send({
      code: 200,
      message: '请求数据成功',
      data: list
    })
  })
};
